package io.renren.common.utils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName WxUtil
 * @Description TODO
 * @Author Jll
 * @Date 2024/6/19 10:32
 * @Version 1.0
 */
public class WxUtil {

    public static String getExpireTime(Integer num, Integer type){
        ZonedDateTime now = ZonedDateTime.now();
        // 加十分钟
        ZonedDateTime tenMinutesLater = now.plusMinutes(num);

        // 创建自定义的日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");

        // 格式化时间为指定格式
        if(type.equals(1)){
            return now.format(formatter);
        }else if(type.equals(2)){
            return tenMinutesLater.format(formatter);
        }
        return now.format(formatter);
    }


    public static String getRequestBody(HttpServletRequest request) throws IOException {
        ServletInputStream stream;
        BufferedReader reader = null;
        StringBuffer sb = new StringBuffer();
        try {
            stream = request.getInputStream();
            // 获取响应
            reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw new IOException("读取返回支付接口数据流出现异常！");
        } finally {
            reader.close();
        }
        return sb.toString();
    }
}
